﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RBOSS.Business.Checkout;
using RBOSS.Business.CustomerModule;
using RBOSS.Business.Views;

namespace RBOSS.Business.Presenters
{
    public class BillingPresenter
    {
        private readonly IBillingView _view;
        private readonly ICheckoutSubsystem _checkoutSubsystem;
        public BillingPresenter(IBillingView view)
        {
            _view = view;
            _checkoutSubsystem = new CheckoutSubsystem();
        }

        public void PopulateInfomation()
        {
            var billingInfo = _checkoutSubsystem.GetBillingInfo();
            if (billingInfo != null)
            {
                _view.Address1 = billingInfo.Address1;
                _view.Address2 = billingInfo.Address2;
                _view.City = billingInfo.City;
                _view.Country = billingInfo.Country;
                _view.State = billingInfo.State;
                _view.Zip = billingInfo.Zip;
            }
        }

        public bool HasLogin()
        {
            var customer = _checkoutSubsystem.GetCustomer();
            if (customer.IsGuest)
            {
                return true;
            }
            return customer.CustomerDetails !=null;
        }

        public IBilling GetBillingInformation()
        {
            return  _checkoutSubsystem.GetBillingInfo();
        }

        public bool IsCreditCardValid()
        {
            
            IBilling billingInfo = new Billing();
            billingInfo.NameOnCard = _view.NameOnCard;
            billingInfo.SecurityNumber = _view.SecurityNumber;
            billingInfo.State = _view.State;
            billingInfo.Address1 = _view.Address1;
            billingInfo.Address2 = _view.Address2;
            billingInfo.City = _view.City;
            billingInfo.Country = _view.Country;
            billingInfo.CreditCardNumber = _view.CreditCardNumber;
            billingInfo.ExpiredDate = _view.ExpiredDate;
            

            return _checkoutSubsystem.IsCreditCardValid(billingInfo);
        }

        public void SaveBillingInformation()
        {
            var billing = new Billing
            {
                Address1 = _view.Address1,
                Address2 = _view.Address2,
                City = _view.City,
                Country = _view.Country,
                CreditCardNumber = _view.CreditCardNumber,
                ExpiredDate =  _view.ExpiredDate,
                NameOnCard = _view.NameOnCard,
                SecurityNumber = _view.SecurityNumber,
                State = _view.State,
                Zip = _view.Zip
            };
            _checkoutSubsystem.SaveBillingInfo(billing);
        }

        public void CopyBilingAddress()
        {
            var creditCartInfo =  _checkoutSubsystem.GetBillingAddressByCreditCard(_view.CreditCardNumber);
            if (creditCartInfo != null)
            {
                _view.Address1 = creditCartInfo.Address1;
                _view.Address2 = creditCartInfo.Address2;
                _view.City = creditCartInfo.City;
                _view.Country = creditCartInfo.Country;
                _view.State = creditCartInfo.State;
                _view.Zip = creditCartInfo.Zip;
            }
        }
    }
}
